import { Injectable } from '@angular/core';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { TableSettingsDialogComponent } from './table-settings-dialog.component';
import { LocalizationService } from '@abp/ng.core';
import { EntityConfig, EntityProperty } from '../entity-config.types';

export interface TableSettingsOptions {
  width?: number;
}

@Injectable({
  providedIn: 'root'
})
export class TableSettingsDialogService {
  constructor(
    private modalService: NzModalService,
    private localizationService: LocalizationService
  ) {}

  show(config: EntityConfig, options?: TableSettingsOptions): NzModalRef {
    return this.modalService.create({
      nzTitle: this.l('BasicArchives::UI:TableSettings.Title'),
      nzContent: TableSettingsDialogComponent,
      nzWidth: options?.width || 800,
      nzFooter: null,
      nzComponentParams: {
        entityConfig: config
      }
    });
  }

  get sortFunction() {
    return (a: EntityProperty, b: EntityProperty) => {
      // 首先按固定属性排序：左侧固定(1) > 不固定(0) > 右侧固定(2)
      if (a.fixed !== b.fixed) {
        if (a.fixed === 1) return -1;
        if (b.fixed === 1) return 1;
        if (a.fixed === 2) return 1;
        if (b.fixed === 2) return -1;
      }
      // 然后按order属性排序
      return a.order - b.order;
    };
  }

  protected l(key: string, ...args: any[]): string {
    return this.localizationService.instant(key, ...args);
  }
}
